package
{
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	import away3d.core.math.Number3D;
	import away3d.core.render.BasicRenderer;
	import away3d.core.render.FindHit;
	import away3d.core.render.Renderer;
	import away3d.events.MouseEvent3D;
	import away3d.extrusions.SegmentsExtrude;
	import away3d.loaders.Obj;
	import away3d.loaders.Object3DLoader;
	import away3d.materials.AlphaBitmapMaterial;
	import away3d.materials.BitmapFileMaterial;
	import away3d.materials.BitmapMaterial;
	import away3d.primitives.Plane;
	import away3d.primitives.Skybox;
	import away3d.primitives.Sphere;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class ExtrudeTest extends Sprite
	{
		private var view:View3D ;
		private var sphere:Sphere;
		private var spaceBox:Skybox;		
		private var scene:Scene3D = new Scene3D();
		private var box :SegmentsExtrude ;
		public var chair:Object3DLoader;
		public var dragPlane:Plane ;
		
		private var down:Boolean = false;
		
		[Embed(source="models/imperio_01.jpg")]
		public var chairPic:Class;
		
		public function ExtrudeTest()
		{
			view = new View3D({x:200, y:100,renderer:Renderer.BASIC,mouseChildren:false ,mouseZeroMove:true});
			addChild(view);
			 
			//magic plane
			var fileMat:BitmapFileMaterial = new BitmapFileMaterial("images/b.jpg");
			var am:AlphaBitmapMaterial = new AlphaBitmapMaterial(fileMat.bitmap, {alpha:0.1, grades:64}); 
//			fileMate.
			
			dragPlane = new Plane({material:am,width:1500, height:1500,pushback:true,bothsides:true,alpha:0.1});

			dragPlane.rotationX = -90;			
			view.scene.addChild(dragPlane);
			dragPlane.addOnMouseDown(press);
			dragPlane.addOnMouseUp(up);
			dragPlane.addOnMouseMove(mv);
			
			
			
			var boxWidth:Number=400;
			var boxHeight:Number=300;
			var boxDepth:Number=300;
			
						
			var aPoints:Array = new Array(); 
			aPoints.push(new Number3D(0-(boxWidth/2), 0, 0-(boxDepth/2)));
			
			aPoints.push(new Number3D((boxWidth/2), 0, 0-(boxDepth/2))); 
			aPoints.push(new Number3D((boxWidth/2), 0, (boxDepth/2))); 
			aPoints.push(new Number3D(0-(boxWidth/2), 0, (boxDepth/2))); 

			//create room	
			var wallThickness:Number = 0;
			box= new SegmentsExtrude(aPoints, { coverall:false,   axis:"y", 
				offset:boxHeight, closepath:true, subdivision:1,   thickness_subdivision:1, flip:false,  
				thickness:wallThickness, bothsides:true,   recenter:true, omit:"bottom"});
			box.y = 0
			box.x = 0;
			box.z = 0;
			box.rotationX = -90;
			view.scene.addChild(box);


			
			
			var bm:BitmapMaterial = new BitmapMaterial(Bitmap(new chairPic()).bitmapData);
			//chair
			chair = Obj.load("models/imperio_01.obj", {material:bm,scaling:5});
			chair.materialLibrary
			chair.name = "chair";
			chair.rotationY = 180;
			chair.addOnSuccess(initObj);
            view.scene.addChild(chair);
			 
//			dragPlane.mouseEnabled = false;    
	     	
			            
			addEventListener(Event.ENTER_FRAME,e);
		}
		
		private function mv(e:MouseEvent3D):void{
			if(down){
				var findHit:FindHit = new FindHit(view,view.primitives,e.sceneX,e.sceneY);
				var x:Number = findHit.sceneX;
				if((-x+chair.handle.maxX)<=box.maxX && (-x+chair.handle.minX)>=box.minX){
					chair.handle.x = -x;
				}
				var z:Number = findHit.sceneZ;
				if((z+chair.handle.maxZ)<=box.maxZ){
					chair.handle.z = z;
				} 
//				var y:Number = findHit.sceneY;
//				if((y+chair.handle.maxY)<=box.maxY && (y + chair.handle.minY)>=box.minY){
//					chair.handle.y = -y;
//				}
			}
		}
		private function initObj(event:Event):void
        {
            chair.handle.y =  -150;
            chair.handle.z = 0;
            chair.handle.mouseEnabled = false;
//            dragPlane.mouseEnabled = false;
        }
        
		private  function press(e:MouseEvent3D):void {
			down = true;
//			dragPlane.mouseEnabled = true;
		}
		
		
		private  function up(e:MouseEvent3D):void{
//			chair.handle.mouseEnabled = true;
//			dragPlane.mouseEnabled = false;
			down = false;
			
		}
	
		private function e(ee:Event):void{
			var r:BasicRenderer = new BasicRenderer();
			view.renderer
			view.render(); 
		}

	}
}


